var baseUrl = require('../../utils/urllink.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    user_id:0,
    store_id:0,
    shop_id:0,
    is_show:false,
    agent_type:0,
    is_mineshow:false,
    showTopTips: false,
    subscribe:0,
    mobile:null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    //登录调用
    getApp().getUserInfo(function (userInfo, openid1) {
      that.setData({
        agent_type: app.globalData.agent_type,
        subscribe: app.globalData.subscribe,
      })
      if (that.data.subscribe == 1) {
        that.setData({
          showTopTips: false,
        })
      } else {
        that.setData({
          showTopTips: true,
        })
      }
      selectuser(that, getApp().globalData.user_id)
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this

    selectuser(that, app.globalData.user_id)
    
  },
  /**
* 页面相关事件处理函数--监听用户下拉动作
*/
  onPullDownRefresh: function () {
    var that = this
    // 获取集合 刷新
    selectuser(that, app.globalData.user_id)
  },

  //跳转到注册绑定页面 判断是否注册手机
  toregister: function (e) {
    var that = this;
    if (that.data.mobile == null || that.data.mobile == ''){
      wx.navigateTo({
        url: '../index/register',
      })
    }else{
      wx.navigateTo({
        url: '../index/user_register',
      })
    }

  },
  shengqikaidian:function(e){
    wx.navigateTo({
      url: '../kaidian/kaidian',
    })
  },
  //跳转到帮助关注页面
  toguanzhu: function (e) {
    wx.navigateTo({
      url: '../index/guanzhu',
    })
  },
  //我的店铺首页
  myshop:function(e){
    var that = this 
    var shop_id = that.data.shop_id
    // wx.navigateTo({
    //   url: '../dianpu/dianpu_goods_list',
    // })

    //跳转到另一个小程序
    wx.navigateToMiniProgram({
      appId: 'wxbceb417181a3492f',
      path: 'pages/shop/detail/index?id='+shop_id,
      extraData: {
        foo: 'bar'
      },
      success(res) {
        // 打开成功
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },



 




  //跳转到店铺页面
  saveshangpu:function(){
    wx.navigateTo({
      url: '../kaidian/saveshangpu'
    })
  },
  //B 跳转到店铺资料页面
  savedianpu:function(){
    wx.navigateTo({
      url: '../dianpu/savedianpu',
    })
  },
  //B 跳转到商品管理页面 只有b有权限
  shangpingguanli:function(){
    wx.navigateTo({
      url: '../dianpu/dianpuguanli',
    })
  },

  //跳转到个人资料
  gerenziliao:function(){
    wx.navigateTo({
      url: '../mine/gerenziliao',
    })
  },
  //跳转到申请开店
  savekaidian:function(){
    wx.navigateTo({
      url: '../kaidian/kaidian',
    })
  },
  //跳转到发布商品页面
  savegoods:function(){
    wx.navigateTo({
      url: '../goods/savegoods?store_id='+this.data.store_id,
    })
  },
  //跳转到商品列表
  goodslist:function(){
    wx.navigateTo({
      url: '../goods/goodslist?store_id=' + this.data.store_id,
    })
  },
  //推广码
  tuiguangma:function(){
    wx.navigateTo({
      url: '../tuiguang/tuiguangma',
    })
  },
  //推广员
  tuiguangyuan: function () {
    wx.navigateTo({
      url: '../tuiguang/tuiguangyuan',
    })
  },
})


function selectuser(that,user_id){
  //请求服务器
  wx.request({
    url: baseUrl +'weapp/Users/info.html',
    data: {
      user_id: user_id,
    },
    method: 'GET',
    header: {
      'content-type': 'application/json'
    }, // 设置请求的 header
    success: function (res) {
      console.log(res.data)
      that.setData({
        userInfo: res.data,
        user_id: res.data.user_id,
        store_id: res.data.store_id,
        is_show:true,
        agent_type: res.data.agent_type,
        is_mineshow:true,
        subscribe: res.data.subscribe,
        shop_id:res.data.shop_id,
        mobile: res.data.mobile,
      })
      getApp().globalData.agent_type = res.data.agent_type;
      getApp().globalData.store_id = res.data.store_id;
      getApp().globalData.subscribe = res.data.subscribe;
      getApp().globalData.shop_id = res.data.shop_id;
      getApp().globalData.mobile = res.data.mobile;
    },
    fail: function () {
    },
    complete: function () {
      // complete
      if (that.data.subscribe == 1) {
        that.setData({
          showTopTips: false,
        })
      } else {
        that.setData({
          showTopTips: true,
        })
      }
    }
  })
}